﻿using Microsoft.Xaml.Behaviors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;


namespace ZuesControls.Behaviors
{
    public class StateTriggerBehavior : Behavior<FrameworkElement>
    {
        private TextBlock _textBlock;

        public static readonly DependencyProperty BindingProperty =
            DependencyProperty.Register(nameof(Binding), typeof(object), typeof(StateTriggerBehavior),
                new PropertyMetadata(null, OnBindingChanged));

        public object Binding
        {
            get => GetValue(BindingProperty);
            set => SetValue(BindingProperty, value);
        }

        public static readonly DependencyProperty TargetStateProperty =
            DependencyProperty.Register(nameof(TargetState), typeof(string), typeof(StateTriggerBehavior), new PropertyMetadata(null, OnTargetStateValueChanged));

        private static void OnTargetStateValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            
        }

        protected override void OnAttached()
        {
            base.OnAttached();

            // 初始应用一次（如模板初始加载）
            UpdateAnimation(Binding);
        }
        private void UpdateAnimation(object? newValue)
        {
            if (AssociatedObject == null) return;

            var targetTextBlock = FindChild<TextBlock>(AssociatedObject, "PART_Header");
            if (targetTextBlock == null) return;

            bool isMatch =
                (Equals(newValue, true) && TargetState == "Expanded") ||
                (Equals(newValue, false) && TargetState == "Collapsed");
            if (isMatch)
            {
                var toOpacity = TargetState == "Expanded" ? 0.0 : 1.0;

                var animation = new DoubleAnimation
                {
                    To = toOpacity,
                    Duration = TimeSpan.FromMilliseconds(300)
                };

                targetTextBlock.BeginAnimation(UIElement.OpacityProperty, animation);
            }
        }

        public string TargetState
        {
            get => (string)GetValue(TargetStateProperty);
            set => SetValue(TargetStateProperty, value);
        }

        private static void OnBindingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is StateTriggerBehavior behavior)
            {
                behavior.UpdateAnimation(e.NewValue);
            }
        }

        private T? FindChild<T>(DependencyObject parent, string childName) where T : FrameworkElement
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);

                if (child is T target && target.Name == childName)
                    return target;

                var result = FindChild<T>(child, childName);
                if (result != null)
                    return result;
            }

            return null;
        }
    }
}
